//
//  ViewController.swift
//  webApp
//
//  Created by Mical_LF on 2017/6/8.
//  Copyright © 2017年 Mical_LF. All rights reserved.
//

import UIKit
import SnapKit
import SwiftyJSON

class ViewController: UIViewController {

    var webVC: WebViewController!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Home"
        self.view.backgroundColor = UIColor.white

        //MARK: 测试JS交互
        self.webVC = WebViewController()
        self.navigationController?.pushViewController(self.webVC, animated: true) 
        
        
        //请求 获取版本更新，信息
        NetworkTools.requestData(.get, URLString: BaseUrl(a: "http://httpbin.org/get")) { (result) in
            let jsonStr  = "{\"version\":\"2.0\",\"url\":\"http://www.baidu.com\"}"
            let jsonData = jsonStr.data(using: String.Encoding.utf8, allowLossyConversion: false)
            let json = JSON(Data: jsonData!)
            let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
            let currentVersionFloat = CFStringGetDoubleValue(currentVersion as CFString)
            let versionFloat = CFStringGetDoubleValue(json["version"].string! as CFString)

            if currentVersionFloat < versionFloat{
                
                let alertController = MyAlertController(title: "版本更新",
                                                        message: "是否更新到最新版本",
                                                        preferredStyle: .alert)
                let okAction = UIAlertAction(title: "确定", style: .default, handler: {
                    action in
                    let url = URL(string: json["url"].string! )
                    UIApplication.shared.openURL(url!)
                })
                
                let delAction = UIAlertAction(title: "取消", style: .default, handler: {
                    action in
                    
                    
                })
                alertController.addAction(okAction)
                alertController.addAction(delAction)
                self.present(alertController, animated: true, completion: nil)
            }
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

